package com.yunquan.ohana.ui.bindEmail

import android.app.Application
import android.os.Bundle
import androidx.compose.ui.res.dimensionResource
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.bus.RxBus
import com.yunquan.ohana.base.bus.RxSubscriptions
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.EventEntity
import com.yunquan.ohana.entity.EventType
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.verifyEmail.VerifyEmailActivity
import com.yunquan.ohana.utils.L
import com.yunquan.ohana.utils.StringUtils
import com.yunquan.ohana.utils.Utils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable
import okhttp3.RequestBody.Companion.toRequestBody

class BindEmailViewModel(application: Application) : BaseViewModel(application) {

    val email = ObservableField<String>()
    val error = ObservableField<String>()

    val onClearClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            email.set("")
        }
    })

    val onContinueClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (StringUtils.isEmpty(email.get())) {
                error.set(Utils.instance.context.getString(R.string.please_enter_email))
                return
            } else if (!StringUtils.isEmail(email.get())) {
                error.set(Utils.instance.context.getString(R.string.email_format_error_please_re_enter))
                return
            }
            error.set(null)
            sendEmailCode()
        }
    })

    override fun onEvent(event: EventEntity) {
        super.onEvent(event)
        if (event.type == EventType.UPDATE_EMAIL) {
            finish()
        }
    }

    private fun sendEmailCode() {
        showLoading()
        val params = mutableMapOf<String, String>()
        params["email"] = email.get()!!
        val json = Gson().toJson(params)
        val body = json.toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.sendEmailCode(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<Boolean>() {
                override fun onSuccess(result: Boolean?) {
                    if (result == true) {
                        val bundle = Bundle()
                        bundle.putString("email", email.get())
                        startActivity(VerifyEmailActivity::class.java, bundle)
                    } else {
                        error.set(Utils.instance.getString(R.string.error_input_email))
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}